package tgc.rj.zz.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import tgc.rj.zz.entity.Journal;
import tgc.rj.zz.entity.SysUser;
import tgc.rj.zz.security.UserUtils;
import tgc.rj.zz.service.JournalService;

@Controller
public class LoginController {
	
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private JournalService service;

	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login() {
		return "login";
	}
	
	/**
	 * 登陆的时候写入日志
	 * @return
	 */
	
	@RequestMapping(value="/main")
	public String main() {
		SysUser user = userUtils.getUser();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String date = df.format(new Date());
		Journal journal = new Journal();
		journal.setDate(date);
		journal.setUsername(user.getUsername());
		journal.setOperationName("登入");
		service.save(journal);
		return "main";
	}
	/**
	 * 
	 * 退出登陆的时候写入日志
	 * @return
	 */
	
	@RequestMapping(value="/logout_tc")
	public String logout() {
		SysUser user = userUtils.getUser();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String date = df.format(new Date());
		Journal journal = new Journal();
		journal.setDate(date);
		journal.setUsername(user.getUsername());
		journal.setOperationName("登出");
		service.save(journal);
		return "redirect:logout";
	}
}
